<template>
  <div>
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>游客管理</el-breadcrumb-item>
      <el-breadcrumb-item>游客列表</el-breadcrumb-item>
    </el-breadcrumb> -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.username">
            <el-button slot="append" icon="el-icon-search" @click="handleSerchUser"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple"></div>
        </el-col>
      </el-row>
      <el-table :data="userList" border>
        <el-table-column type="index" width="50" label="#">
        </el-table-column>
        <el-table-column label="头像" width="100">
          <template v-slot:default="scope">
            <el-image :src="'https://'+scope.row.avatar" style="width:60px;height:60px;border-radius: 50%;"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="用户名" width="200" prop="nick_name"></el-table-column>
        <el-table-column label="id" prop="id" width="50"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="日期" prop="create_time"></el-table-column>
        <el-table-column label="状态" width="100">
          <template v-slot:default="scope">
            <el-tooltip class="item" effect="dark" content="修改状态" placement="top">
              <el-switch v-model="scope.row.isDelete" active-color="#13ce66" inactive-color="#ccc" @change="editUser(scope.row)"></el-switch>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="content" width="100">
          <template v-slot:default="scope">
            <el-button type="danger" @click="deleteUserTap(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total='total' :page-size="queryInfo.pagesize" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getUsersApi, editUserApi, deleteUserApi } from '../../request/apis'
export default {
  data () {
    return {
      queryInfo: {
        username: '',
        pagenum: 1,
        pagesize: 8
      },
      total: 0,
      userList: []
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getUserList()
    },
    getUserList () {
      getUsersApi({ params: this.queryInfo }).then(res => {
        // const data = res.data
        if (res.meta.status !== 200) {
          return this.$message.error('获取用户列表失败')
        }
        res.data.users.forEach((value, index) => {
          value.isDelete = value.status !== 0
          value.sex = value.gender === 0 ? '女' : '男'
        })
        this.userList = res.data.users
        this.total = res.data.total
      })
    },
    async editUser (userInfo) {
      const confirmResult = await this.$confirm('修改该用户权限', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        userInfo.isDelete = !userInfo.isDelete
        return err
      })
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消')
      }
      const userId = userInfo.id
      const userResult = await editUserApi(userId)
      if (userResult.meta.status !== 200) {
        userInfo.isDelete = !userInfo.isDelete
        return this.$message.error('修改用户权限失败')
      }
      this.$message.success('修改成功！')
    },
    // 搜索用户
    handleSerchUser () {
      this.queryInfo.pagenum = 1
      this.getUserList()
    },
    // 删除用户
    async deleteUserTap (id) {
      const { data, meta } = await deleteUserApi({ params: { id } })
      if (meta.status === 200) {
        this.$message.success('删除成功')
        if ((data.total % this.queryInfo.perpage) === 0) {
          this.queryInfo.pagenum > 1 ? this.queryInfo.pagenum -= 1 : this.queryInfo.pagenum = 1
        }
        this.getUserList()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
}
</style>
